package com.miyake.loveviewweb.client.viewer;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.commonui.ComboBoxEx;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.model.PluginModel;

public abstract class ChartToolbar extends Composite {
    public static final String FLASH_CHART = "Flash Chart";
    public static final String SIMPLE_DYNAMIC_CHART = "Simple Dynamic Chart";
    private HorizontalPanel toolPanel;
    private ListBox columnsBox;
    
    abstract void onColumnsChange(Integer column);
    abstract void onChartTypeChange(String chartType, PluginModel chartPlugin);
    abstract void onChartSizeChange(Integer width, Integer height);
    abstract void onEditEnabled(boolean enabled);
    abstract void onDelete();
    
    protected Widget getAdditional() {
        return new HTML("");
    }
    
    public ChartToolbar() {
        toolPanel = new HorizontalPanel();
        
        toolPanel.add(getColumnNumberComboBox());
        
        toolPanel.add(getChartSizeComboBox());
        
        final HorizontalPanel editBar = new HorizontalPanel();
        editBar.setVisible(false);
        CheckBox editCheck = new CheckBox("Edit");
        toolPanel.add(editCheck);
        toolPanel.add(editBar);
        editCheck.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

            public void onValueChange(ValueChangeEvent<Boolean> event) {
                onEditEnabled(event.getValue());
                editBar.setVisible(event.getValue());
            }
            
        });
        
        Button deleteButton = new Button("Delete");
        editBar.add(deleteButton);
        deleteButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                onDelete();
            }
        });
        
        toolPanel.add(getAdditional());
        initWidget(toolPanel);
    }
    private ListBox getColumnNumberComboBox() {
        columnsBox = new ListBox();
        columnsBox.addItem("1");
        columnsBox.addItem("2");
        columnsBox.addItem("3");
        columnsBox.addItem("4");
        columnsBox.addItem("5");
        columnsBox.addItem("6");
        columnsBox.addItem("7");
        columnsBox.addItem("8");
        toolPanel.add(new HTML("Colum:"));
        toolPanel.add(columnsBox);
        columnsBox.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                onColumnsChange(columnsBox.getSelectedIndex()+1);
            }
        });
        
        return columnsBox;
    }
    
    private ListBox getChartSizeComboBox() {
        final ListBox sizeBox = new ListBox();
        sizeBox.addItem("100x80");
        sizeBox.addItem("200x150");
        sizeBox.addItem("300x200");
        sizeBox.addItem("400x300");
        sizeBox.addItem("500x400");
        
        sizeBox.addChangeHandler(new ChangeHandler() {

            public void onChange(ChangeEvent event) {
                String value = sizeBox.getValue(sizeBox.getSelectedIndex());
                Integer width = Integer.valueOf(value.split("x")[0]);
                Integer height =Integer.valueOf(value.split("x")[1]);
                onChartSizeChange(width, height);
            }
            
        });
        return sizeBox;
    }
    
    private ComboBoxEx<PluginModel> getChartComboBox() {
        final ComboBoxEx<PluginModel> listBox = createListBox();
        listBox.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {  

                if (listBox.getSelectedIndex() == listBox.getItemCount()-1) {
                    Window.open(GWT.getHostPageBaseURL()+"?startup=pluginMarket&filter=Chart", "_blank", "");     
                }
                else {
                    onChartTypeChange(listBox.getValue(listBox.getSelectedIndex()), listBox.getSelectedObject());
                }
            }
        });
        
        return listBox;
    }
    private ComboBoxEx<PluginModel> createListBox() {
        final ComboBoxEx<PluginModel> listBox = new ComboBoxEx<PluginModel>();
        
        listBox.addItem(SIMPLE_DYNAMIC_CHART);
        listBox.addItem(FLASH_CHART);
        DataService.Util.getInstance().getInstalledPlugins("Chart", new AsyncCallback<List<PluginModel>>() {

            public void onFailure(Throwable caught) { 
            }

            public void onSuccess(List<PluginModel> result) {
                for (PluginModel pluginModel : result) {
                    listBox.addItem(pluginModel.getServiceName(), pluginModel);
                }
             
                listBox.addItem("--Search other charts--");
            }
            
        });
        return listBox;
    }
    
    public void addWidget(Widget widget) {
        toolPanel.add(widget);
    }
    public void setColumns(Integer columns) {
        columnsBox.setSelectedIndex(columns-1);
    }
}
